package com.lbssoft.gpsbd.common;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import java.util.Map;

/**
 * Created by nczc on 2018/11/23.
 */
public class MessageUtil {

    public static boolean sendNotificationWithAlias(String title, String notification, String alias, Map<String,String> extras, String sound){
        try {
            //gpsbd
            JPushClient client = new JPushClient("", "", 3);
            PushPayload payload = PushPayload
                    .newBuilder()
                    .setPlatform(Platform.all())
                    .setAudience(Audience.alias(alias.replace(".", "_")))
                    .setNotification(
                            Notification
                                    .newBuilder()
                                    .addPlatformNotification(
                                            IosNotification.newBuilder()
                                                    .setAlert(notification)
                                                    .setSound(sound).setBadge(1)
                                                    .addExtras(extras).build())
                                    .addPlatformNotification(
                                            AndroidNotification.newBuilder()
                                                    .setAlert(notification)
                                                    .setTitle(title).addExtras(extras)
                                                    .build())

                                    .build()).setOptions(Options.newBuilder()
                            .setApnsProduction(true)//true生产环境 false 开发环境
                            .build()).build();

            PushResult result =client.sendPush(payload);
            //System.out.println(result.isResultOK());
            return result.isResultOK();
        } catch (APIConnectionException e) {
            //return false;
            // e.printStackTrace();
        } catch (APIRequestException e) {
            //return false;
            //  e.printStackTrace();
        }
        return false;
    }
}
